﻿define(function (require, exports, module) {
    var Canvas = require('canvas');
    var Config = require('config');
    var Player = require('player');
    var ImageStorage = require('imageStorage');
    var Request = require('request');
    var m_cityCanvas = Canvas.Get(4);
    var m_width = 800;
    var m_height = 600;
    var m_toMapSrc = 'images/city/toMap.gif';
    var m_insideSrc = 'images/city/inside.gif';
    var m_map;

    function City(data, map) {
        this.Data = data;
        m_map || (m_map = map);
    };

    City.prototype.GetLeft = function (left) {
        'number' == typeof left || (left = 0);
        return this.Data.X - left;
    };

    City.prototype.GetTop = function (top) {
        'number' == typeof top || (top = 0);
        return this.Data.Y - top;
    };

    City.prototype.GetRight = function () {
        return this.GetLeft() + this.Data.Width;
    };

    City.prototype.GetBottom = function () {
        return this.GetTop() + this.Data.Height;
    };

    City.prototype.Render = function (left, top) {
        RenderAsync(this, left, top).start();
    };

    var RenderAsync = eval(Wind.compile('async', function (self, left, top) {
        var img = $await(ImageStorage.GetAsync(self.Data.Img));
        m_cityCanvas.Call('drawImage', img, self.GetLeft(left), self.GetTop(top), self.Data.Width, self.Data.Height);
    }));

    City.prototype.IsOver = function (x, y) {
        return this.GetLeft() <= x && x < this.GetRight() && this.GetTop() <= y && y < this.GetBottom();
    };

    City.prototype.Enter = function (map) {
        var self = this;
        Request.Ajax({
            url: 'getCityDetail.do',
            data: { id: this.Data.Id },
            success: function (response) {
                if (response.Success) {
                    m_map.Hide();
                    Player.Hide();
                    self.RenderDetail(response.Data);
                }
                else {
                    alert(response.Message);
                }
            }
        });
    };

    City.prototype.RenderDetail = function (data) {
        m_cityCanvas.Call('save');
        m_cityCanvas.Set('fillStyle', 'black')
            .Set('textBaseline', 'top');
        m_cityCanvas.Call('fillText', '兵力：', 10, 10);
        m_cityCanvas.Call('fillText', '耐久：', 10, 30);
        m_cityCanvas.Call('fillText', '攻击力：', 10, 50);
        m_cityCanvas.Set('textAlign', 'right');
        m_cityCanvas.Call('fillText', data.Troops + '/' + this.Data.Troops, 150, 10);
        m_cityCanvas.Call('fillText', data.Durable + '/' + this.Data.Durable, 150, 30);
        m_cityCanvas.Call('fillText', data.Atk, 150, 50);

        RenderDetailAsync(this).start();
    };

    var RenderDetailAsync = eval(Wind.compile('async', function (_this) {
        var insideImg = $await(ImageStorage.GetAsync(m_insideSrc));
        m_cityCanvas.Call('drawImage', insideImg, 0, 0, m_width, m_height);
        var toMapImg = $await(ImageStorage.GetAsync(m_toMapSrc));
        m_cityCanvas.Call('drawImage', toMapImg, m_width - toMapImg.width, 0);
        m_cityCanvas.Call('restore');
        m_cityCanvas.Bind(module.id, {
            click: function (e) {
                if ((m_width - toMapImg.width) <= e.clientX && e.clientX <= m_width &&
                            0 <= e.clientY && e.clientY <= toMapImg.height) {
                    _this.Quit();
                }
            }
        });
    }));

    City.prototype.Quit = function (map) {
        m_map.Show();
        Player.Show();
        m_cityCanvas.Unbind(module.id);
    };

    module.exports = City;
});